package com.tutucms.service.admin.configuration;

import com.tutucms.service.util.wechatpay.IWXPayDomain;
import com.tutucms.service.util.wechatpay.WXPayConfig;
import com.tutucms.service.util.wechatpay.WXPayConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;

/**
 * @title:
 * @author: wangxudong
 * @date: 2019/6/18 14:15
 * @version: 1.0
 * @modified :
 */
@Slf4j
@Component
public class WxPayConfig extends WXPayConfig {

//    private static final String CREDENTIAL_FILE = "wxpay-credential.crt";

    @Value("${app.wechat.wxpay.appId}")
    private String appId;
    @Value("${app.wechat.wxpay.mchId}")
    private String mchId;
    @Value("${app.wechat.wxpay.secretKey}")
    private String secretKey;
    @Value("${app.wechat.wxpay.notifyUrl}")
    private String notifyUrl;


//    private byte[] certData;


    public WxPayConfig() throws IOException {

        //目前支付接口用不到证书，证书针对退款等功能
//        InputStream certStream = WeChatPayConfig.class.getClassLoader().getResourceAsStream(CREDENTIAL_FILE);
//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//
//        log.info("读取微信支付证书");
//        try {
//            byte[] buffer = new byte[1024];
//            int num = certStream.read(buffer);
//            while (num != -1) {
//                baos.write(buffer, 0, num);
//                num = certStream.read(buffer);
//            }
//            baos.flush();
//
//            this.certData = baos.toByteArray();
//
//            certStream.close();
//        } catch (IOException e) {
//            log.error("微信支付证书读取错误!" + e.getMessage(), e);
//            throw e;
//        }
    }

    @Override
    public String getAppID() {

        return this.appId;
    }

    @Override
    public String getMchID() {

        return this.mchId;
    }

    @Override
    public String getKey() {

        return this.secretKey;
    }

    @Override
    public InputStream getCertStream() {

        // return new ByteArrayInputStream(this.certData);
        return null;
    }

    @Override
    public IWXPayDomain getWXPayDomain() {
        IWXPayDomain iwxPayDomain = new IWXPayDomain() {

            public void report(String domain, long elapsedTimeMillis, Exception ex) {

            }

            public DomainInfo getDomain(WXPayConfig config) {
                return new IWXPayDomain.DomainInfo(WXPayConstants.DOMAIN_API, true);
            }
        };
        return iwxPayDomain;
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 10000;
    }

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public void setMchId(String mchId) {
        this.mchId = mchId;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }
}
